<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 用户管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>用户信息</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.name" placeholder="请输入真实姓名" class="handle-input mr10" clearable></el-input>
                <el-select v-model="query.identity" placeholder="请选择用户身份" clearable>
                    <el-option v-for="item in items" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
                <el-button style="display: inline-block; margin-left: 10px" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table v-loading="loading" :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="id" label="编号" width="50" align="center"></el-table-column>
                <el-table-column prop="openId" label="用户标识" show-overflow-tooltip=true width="100"></el-table-column>
                <el-table-column prop="nickName" label="微信昵称"></el-table-column>
                <el-table-column prop="gender" label="性别" :formatter="handleGender"></el-table-column>
                <el-table-column prop="identity" label="身份" :formatter="handleIdentity"></el-table-column>
                <el-table-column prop="disabled" label="状态" :formatter="handleState"></el-table-column>
                <el-table-column prop="curMoney" label="余额(分)"></el-table-column>
                <el-table-column prop="allMoney" label="总收入(分)" width="100"></el-table-column>
                <el-table-column prop="createTime" label="添加时间" show-overflow-tooltip=true width="110"></el-table-column>
                <el-table-column prop="updateTime" label="修改时间" show-overflow-tooltip=true width="110"></el-table-column>
                <el-table-column label="操作" width="180" align="center" v-if="modifyTag">
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleDetail(scope.$index, scope.row)">详情
                        </el-button>
                        <el-button v-show="scope.row.disabled === 0" type="text" icon="el-icon-delete" class="red" @click="handleDisable(scope.$index, scope.row)">禁用</el-button>
                        <el-button v-show="scope.row.disabled === 1" type="text" icon="el-icon-circle-plus" class="green" @click="handleDisable(scope.$index, scope.row)">启用</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageNum"
                    :page-size="query.pageSize" :total="query.total" @current-change="handlePageChange"></el-pagination>
            </div>
        </div>

        <!-- 详情弹出框 -->
        <el-dialog title="详情" v-model="detailVisible" width="30%">
            <div style="text-align: center">
                <strong>用户未认证</strong>
            </div>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="primary" @click="detailVisible = false">关 闭</el-button>
                </span>
            </template>
        </el-dialog>

        <el-dialog title="详情" v-model="detailVisible2" width="50%">
            <el-row style="margin-bottom: 5px">真实姓名: {{detailData.name}}</el-row>
            <el-row style="margin-bottom: 5px">手机号码: {{detailData.phone}}</el-row>
            <el-row style="margin-bottom: 5px">所在省份: {{detailData.province}}</el-row>
            <el-row style="margin-bottom: 5px">所在城市: {{detailData.city}}</el-row>
            <el-row style="margin-bottom: 5px">所在城市: {{detailData.town}}</el-row>
            <el-row style="margin-bottom: 5px">详细地址: {{detailData.detailAddr}}</el-row>
            <el-row>
                认证图片: <el-image fit="fit" :src="detailData.certificationUrl"></el-image>
            </el-row>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="primary" @click="detailVisible2 = false">关 闭</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { onActivated, ref, reactive, inject } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getUserList, getUserDetail, disableUser } from "../../api";

export default {
    name: "user",
    setup() {
        const loading = ref(true);
        const query = reactive({
            pageNum: 1,
            pageSize: 10,
            total: 0,
            name: '',
            identity: null
        });
        const items = [
                {id: 0, name: "游客"},
                {id: 1, name: "用户"},
                {id: 2, name: "回收员"},
        ];
        const tableData = ref([]);
        const pageTotal = ref(0);
        const reload  = inject("reload");
        let modifyTag = true;
        const detailVisible = ref(false);
        const detailVisible2 = ref(false);
        const initTag = () => {
            let role = localStorage.getItem("role");
            if (role === 1 || role === '1') {
                modifyTag = false;
            }
        };
        initTag();

        // 获取表格数据
        const getData = () => {
            loading.value = true;
            getUserList(query).then((res) => {
                tableData.value = res.data.content;
                query.total = res.data.count;
                loading.value = false;
            });
        };
        getData();

        // 查询操作
        const handleSearch = () => {
            query.pageNum = 1;
            getData();
        };
        // 分页导航
        const handlePageChange = (val) => {
            query.pageNum = val;
            getData();
        };


        // 禁用操作
        const handleDisable = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定进行该操作吗？", "提示", {
                type: "warning",
            }).then(() => {
                disableUser(row.id).then(res => {
                    if (res.code === 200) {
                        reload();
                        ElMessage.success('操作成功');
                    } else {
                        console.log("[禁用用户] 失败: " + res.message);
                        ElMessage.error('服务器繁忙，请稍后重试');
                    }
                })
            }).catch(() => {});
        };

        const detailData = ref([]);
        // 表格详情弹窗
        const handleDetail = (index, row) => {
            if (row.identity === 0 || row.identity === '0') {
                detailVisible.value = true;
                return ;
            }
            console.log(row);
            getUserDetail(row.id).then(res => {
                if (res.code === 200) {
                    console.log(res);
                    detailData.value = res.data;
                    detailVisible2.value = true;
                } else {
                    ElMessage.error('服务器繁忙，请稍后重试');
                }
            })
        };

        const handleState = (row, column) => {
            return row.disabled === 0 ? "正常" : "禁用";
        };
        const handleGender = (row, column) => {
            return row.gender === 0 ? "男" : "女";
        };
        const handleIdentity = (row, column) => {
            if (row.identity === 0) return "游客";
            else if (row.identity === 1) return "用户";
            else return "回收员";
        };

        onActivated(() => {
            console.log("Medicine onActivated!");
            getData();
        });

        return {
            query,
            tableData,
            pageTotal,
            detailVisible,
            detailVisible2,
            modifyTag,
            detailData,
            items,
            loading,
            handleSearch,
            handlePageChange,
            handleDetail,
            handleState,
            handleGender,
            handleIdentity,
            handleDisable
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
</style>
